import 'package:aidou/style/style.dart';
import 'package:flutter/material.dart';

class VideoButtonColumn extends StatelessWidget {
  final double? bottomPadding;
  final bool isFavorite;
  final Function()? onFavorite;
  final Function()? onComment;
  final Function()? onShare;
  final Function()? onAvatar;
  const VideoButtonColumn({
    Key? key,
    this.bottomPadding,
    this.onFavorite,
    this.onComment,
    this.onShare,
    this.isFavorite: false,
    this.onAvatar,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: SysSize.avatar,
      margin: EdgeInsets.only(
        bottom: bottomPadding ?? 0,
        right: 10,
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: <Widget>[
          Avatar(),
          FavoriteIcon(
            onFavorite: onFavorite,
            isFavorite: isFavorite,
          ),
          _IconButton(
            icon: IconToText(Icons.comment, size: SysSize.iconBig - 4),
            text: '444',
            onTap: onComment,
          ),
          _IconButton(
            icon: IconToText(Icons.share, size: SysSize.iconBig - 4),
            text: '444',
            onTap: onShare,
          ),
          Container(
            width: SysSize.avatar,
            height: SysSize.avatar,
            margin: EdgeInsets.only(top: 10),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(SysSize.avatar / 2.0),
            ),
          )
        ],
      ),
    );
  }
}

// 头像
class Avatar extends StatelessWidget {
  const Avatar({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 用户头像
    Widget avatar = Container(
      width: SysSize.avatar,
      height: SysSize.avatar,
      margin: EdgeInsets.only(bottom: 10),
      decoration: BoxDecoration(
        border: Border.all(
          color: Colors.white,
          width: 1,
        ),
        borderRadius: BorderRadius.circular(SysSize.avatar / 2.0),
        color: Colors.orange,
      ),
      child: ClipOval(
        child: Image.network(
          'https://guangbei-video.obs.cn-east-3.myhuaweicloud.com/images/20211102/93c901fac44a47cb92261da4fbba6c6e.png',
          fit: BoxFit.cover,
        ),
      ),
    );
    // 关注按钮
    Widget addButton = Container(
        width: 20,
        height: 20,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          color: ColorPlate.red,
        ),
        child: InkResponse(
          child: Icon(
            Icons.add,
            size: 18,
            color: ColorPlate.white,
          ),
          onTap: () {
            print('关注');
          },
        ));
    return Container(
      width: SysSize.avatar,
      height: 66,
      margin: EdgeInsets.only(bottom: 6),
      child: Stack(
        alignment: Alignment.bottomCenter,
        children: <Widget>[avatar, addButton],
      ),
    );
  }
}

// 喜欢小部件
class FavoriteIcon extends StatelessWidget {
  final bool? isFavorite;
  final Function()? onFavorite;
  const FavoriteIcon({
    Key? key,
    required this.onFavorite,
    this.isFavorite,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _IconButton(
      icon: IconToText(Icons.favorite,
          size: SysSize.iconBig, color: isFavorite! ? ColorPlate.red : null),
      text: '1.0w',
      onTap: onFavorite,
    );
  }
}

// 把IconData转换为文字，使其可以使用文字样式
class IconToText extends StatelessWidget {
  final IconData? icon;
  final TextStyle? style;
  final double? size;
  final Color? color;

  const IconToText(
    this.icon, {
    Key? key,
    this.style,
    this.size,
    this.color,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text(
      String.fromCharCode(icon!.codePoint),
      style: style ??
          TextStyle(
            fontFamily: 'MaterialIcons',
            fontSize: size ?? 30,
            inherit: true,
            color: color ?? ColorPlate.white,
          ),
    );
  }
}

// 图标按钮小部件
class _IconButton extends StatelessWidget {
  final Widget? icon;
  final String? text;
  final Function()? onTap;
  const _IconButton({
    Key? key,
    this.icon,
    this.text,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var shadowStyle = TextStyle(
      shadows: [
        Shadow(
          color: Colors.black.withOpacity(0.15),
          offset: Offset(0, 1),
          blurRadius: 1,
        ),
      ],
    );
    Widget body = Column(
      children: <Widget>[
        InkResponse(
          child: icon ?? Container(),
          onTap: onTap,
        ),
        Container(height: 2),
        Text(
          text ?? '??',
          style: TextStyle(
            fontWeight: FontWeight.normal,
            fontSize: SysSize.small,
            color: ColorPlate.white,
          ),
        ),
      ],
    );
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10),
      child: DefaultTextStyle(child: body, style: shadowStyle),
    );
  }
}
